"""
Generates lexicon-related data tables for Medical Term Identification according to the
tokeniser and stop-list.
"""

import re
import sys
from sets import Set
from DatabaseConnector import connection

_DEBUG_ = True

class Lexicon (object):

    instance = None
    initialised = False

    def __new__(cls, *args, **kargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls, *args, **kargs)
        return cls.instance

    lex_ids = {}
    lex_count = {}

    def __init__(self, lex_name):
        self.lex_name = lex_name.strip()
        if not self.initialised:
            self._init()
            self.initialised = True

    def _load_lexicon(self):

        if _DEBUG_:
            sys.stderr.write("Use lexicon: %s\n" % self.lex_name)

        if _DEBUG_:
            sys.stderr.write("Loading lexids..")
        cursor = connection.cursor()
        cursor.execute("SELECT distinct ids, term FROM snomed.sct_lexicon_20070731")
        for i, tuple in enumerate(cursor.fetchall()):
            if _DEBUG_:
                 if i % 10000 == 0:
                     sys.stderr.write(".")
            ids = list(str(tuple[0]).split(':'))
            lexeme = tuple[1]
            self.lex_ids[lexeme] = ids
        cursor.close()
#        print str(ids), lexeme
#####
#        i = 0
#        for tuple in cursor.fetchall():
#             if _DEBUG_:
#                 if i % 10000 == 0:
#                     sys.stderr.write(".")
#             ids = str(tuple[0]).strip('L')
#             lexeme = tuple[1]
#             if self.lex_ids.has_key(lexeme):
#                self.lex_ids[lexeme].append(ids)
#             else:
#                  i = i + 1
#                  self.lex_ids[lexeme] = list(ids)
                  # print self.lex_ids
 #         print lexeme, self.lex_ids[lexeme]
#        
#####     



#Loading the lexicon from a file on the local disk
#        for i, row in enumerate(file("./%s/lexids" % self.lex_name, "r")):
#            if _DEBUG_:
#                if i % 10000 == 0:
#                    sys.stderr.write(".")
#
#            row = row.strip()
#            lexeme, ids = row.split("\t")
#            self.lex_ids[lexeme] = ids.split(":")
#        
#        print lexeme, self.lex_ids[lexeme]

        if _DEBUG_:
            sys.stderr.write(" %s entries\n" % i)
#
        if _DEBUG_:
            sys.stderr.write("Loading lexcount..")

        cursor = connection.cursor()
        cursor.execute("SELECT * FROM snomed.sct_term_count_20070731")
        i = 0
        for tuple in cursor.fetchall():
             i = i + 1
             if _DEBUG_:
                 if i % 10000 == 0:
                     sys.stderr.write(".")
             did = str(tuple[0]).strip('L')
             tkncount = str(tuple[1])
             stopratio = float(tuple[2])
             self.lex_count[did] = (tkncount,  stopratio)
 
        cursor.close()
#         print did, tkncount, stopratio, self.lex_count[did]
#         print self.lex_count
#Loading the lexicon_count from a file on the local disk
#        for i, row in enumerate(file("./%s/lexcount" % self.lex_name, "r")):
#            if _DEBUG_:
#                if i % 10000 == 0:
#                    sys.stderr.write(".")
#
#            row = row.strip()
#            did, tkncount, stopratio = row.split("\t")
#            stopratio = float(stopratio)
#            self.lex_count[did] = (tkncount, stopratio)
        
#        print did, tkncount, stopratio, self.lex_count[did]
#        print self.lex_count
        if _DEBUG_:
            sys.stderr.write(" %s entries\n" % i)

    def _init(self):
        if not self.initialised:
            self._load_lexicon()
            self.initialised = True

    def get_lex_ids(self, lexeme, dids=None):
        if not dids:
            return self.lex_ids.get(lexeme, [])
        else:
            res = Set(self.lex_ids.get(lexeme, []))
            return list(res.intersection(Set(dids)))
            
    def get_lex_count(self, did):
        if self.lex_count.has_key(did):
            return int(self.lex_count[did][0])
        else:
            return None

    def get_stop_ratio(self, did):
        if self.lex_count.has_key(did):
            return int(self.lex_count[did][1])
        else:
            return None

#if __name__ == "__main__":
#   lexicon = Lexicon("LexSCT")
